﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Components.Enum;
using UI.Lib;
using UI.Lib.Factory;
using Components;
using Controls;
using System.Collections;

namespace UI.ParentForm
{
    public partial class BaseSedit : BaseMedit
    {
        public BaseSedit()
        {
            InitializeComponent();

            if (base.isDebug == false)
            {
                LoadSummaryControl();
            }
            AddToolButton();
        }

        public BaseSedit(string modulecode, string action)
            : base(modulecode, action)
        {
            InitializeComponent();

            tspDtlTop.Visible = false; //隐藏明细栏中的工具条
            this.scrollBar1.Visible = false;
            this.scrollBar1.Text = "";

            if (base.isDebug == false)
            {
                LoadSummaryControl();
            }

            AddToolButton();
        }

        protected override void ResetSize()
        {
            int winHeight = this.ClientSize.Height;

            int summaryHeight = this.tbSummary.Height;
            summaryHeight = Math.Max(summaryHeight, 60);
            this.tbSummary.Size = new Size(this.ClientSize.Width - 40, summaryHeight);

            this.panDtl.Location = new Point(this.panDtl.Location.X, tspTop.Height + summaryHeight + 50);
            this.panDtl.Size = new Size(this.tbSummary.Width, 240); //明细栏固定高度

            //设置汇总栏位置和高度
            this.gpMain.Location = new Point(this.gpMain.Location.X, this.panDtl.Location.Y + this.panDtl.Height + 10);
            this.gpMain.Size = new Size(this.tbSummary.Width, winHeight - this.panDtl.Height - tspTop.Height - this.tbSummary.Height - 100);
        }

        protected override void RefreshControl()
        {
            base.RefreshControl(); //刷新控件

            this.tbMain.SetControlState(this.Action);
        }

        private void tbMain_AfterCreateControl(object sender, Controls.Args.SummaryArgs e)
        {
            Control c = sender as Control;
            if (c == null) return;

            //添加默认事件
            AddEvent(c);

            AfterCreateControlInSummary(c.Name, c);
        }

        protected virtual void AfterCreateControlInSummary(string colName, Control c) { }

        protected void LoadSummaryControl()
        {
            string page = this.GetType().FullName;

            if (!BeforeCreateChildControl()) return;

            //加载页面概要设计信息
            CommonObject obj = PageCache.GetSys5A00(page, PageControlType.MainCtl);

            if (obj.IsEmptyData) return; //页面未设置概要信息
            int colsize = Convert.ToInt32(obj.GetValue("colsize"));
            string text = Validation.ToString(obj.GetValue("remark"));

            //加载页面控件数据设计
            DataTable dt = PageCache.GetLayout(page, PageControlType.MainCtl);

            if (null == dt || 0 == dt.Rows.Count) return;

            //加载概要
            tbMain.DataSource = dt;
            tbMain.AutoHeight = true;
            tbMain.ColSize = colsize;
            tbMain.Bind();

            AfterLoadSControl();
        }

        protected virtual bool BeforeCreateChildControl() { return true; }

        protected virtual void AfterLoadSControl() { }

        protected virtual void AddToolButton() { }

        /// <summary>
        /// 页面加载完成后，自动重置一次控件状态
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if ("V" == this.Action) //显示状态下不显示保存按钮
            {
                tspSave.Visible = false;
            }

            if (MaxStateFlag)
            {
                this.WindowState = FormWindowState.Maximized;
            }

            this.tbMain.FormatStyle();
            this.tbSummary.FormatStyle();
            this.ResetSize();
            this.tbMain.SaveBak(); //保存当前数据，用于做是否修改的提示
        }
    }
}
